﻿Imports System.IO
Imports System.Web.UI.DataVisualization.Charting
Imports System.Data.SqlClient
Imports System.Data

Partial Class Reportes_DetallePriorizacion
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Page.IsPostBack = False Then
            If String.IsNullOrEmpty(Request.QueryString.Item("Idhogar")) = False Then
                hdIdhogar.Value = Request.QueryString.Item("Idhogar")
            End If

            'cargar proyectos
            traerDatosHogar()
            generarGrafica()
            DetalleIntegrantes()
            PsicoSocialHogar()
            PsicoSocialIndividual()
        End If


    End Sub
    Sub traerDatosHogar()
        Using bd As New BdHogaresDataContext
            Dim a = (From h In bd.Web_FormularioIdentificacionHogars
                    Where h.idHogar = hdIdhogar.Value
                    Select h.CodCoba, h.Web_Proyecto.CodigoProyecto).FirstOrDefault
            lblCodigoCoba.Text = a.CodCoba
            lblCodProyecto.Text = a.CodigoProyecto
        End Using
    End Sub
    Sub generarGrafica()
        Using cnn As New SqlConnection(ConfigurationManager.ConnectionStrings("CNNdb").ConnectionString)
            cnn.Open()

            Using cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand("rptCategoriazacionXHogar", cnn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@idhogar", CInt(hdIdhogar.Value))

                Dim reader As IDataReader = cmd.ExecuteReader()

                grdResultados.DataSource = reader
                grdResultados.DataBind()

                reader.NextResult()

                Chart1.DataSource = reader
                Chart1.ImageType = DataVisualization.Charting.ChartImageType.Png
                With Chart1.Series("Default")
                    .XValueMember = "Identificador"
                    .YValueMembers = "li"

                    .ChartType = SeriesChartType.Radar
                    .LabelFormat = "P2"
                    .Item("RadarDrawingStyle") = "Area"
                    .Item("AreaDrawingStyle") = "Polygon"
                    .Item("CircularLabelsStyle") = "Horizontal"

                End With


                With Chart1.Series("v1")
                    .XValueMember = "Identificador"
                    .YValueMembers = "v1"

                    .ChartType = SeriesChartType.Radar
                    .LabelFormat = "P2"
                    .Item("RadarDrawingStyle") = "Area"
                    .Item("AreaDrawingStyle") = "Polygon"
                    .Item("CircularLabelsStyle") = "Horizontal"

                End With

                With Chart1.Series("v2")
                    .XValueMember = "Identificador"
                    .YValueMembers = "v2"

                    .ChartType = SeriesChartType.Radar
                    .LabelFormat = "P2"
                    .Item("RadarDrawingStyle") = "Area"
                    .Item("AreaDrawingStyle") = "Polygon"
                    .Item("CircularLabelsStyle") = "Horizontal"
                End With

                With Chart1.Series("v3")
                    .XValueMember = "Identificador"
                    .YValueMembers = "v3"

                    .ChartType = SeriesChartType.Radar
                    .LabelFormat = "P2"
                    .Item("RadarDrawingStyle") = "Area"
                    .Item("AreaDrawingStyle") = "Polygon"
                    .Item("CircularLabelsStyle") = "Horizontal"
                End With
                'Chart1.Series("Default").XValueMember = "Identificador"
                'Chart1.Series("Default").YValueMembers = "Porcentaje"
                Chart1.DataBind()
                'Chart1.Series("Default").ChartType = SeriesChartType.Radar
                ''Chart1.Series("Default").ChartType = SeriesChartType.Line
                'Chart1.Series("Default").LabelFormat = "P2"
                'Chart1.Series("Default")("RadarDrawingStyle") = "Area"
                'Chart1.Series("Default")("AreaDrawingStyle") = "Polygon"
                'Chart1.Series("Default")("CircularLabelsStyle") = "Horizontal"



                Chart1.ChartAreas("ChartArea1").AxisY.LabelStyle.Format = "P0"
                Chart1.ChartAreas("ChartArea1").AxisY.Maximum = 1
                Chart1.ChartAreas("ChartArea1").Area3DStyle.Enable3D = False




            End Using

            cnn.Close()

        End Using

        Using cnn As New SqlConnection(ConfigurationManager.ConnectionStrings("CNNdb").ConnectionString)
            cnn.Open()

            Using cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand("rptCategoriazacionXHogar", cnn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@idhogar", CInt(hdIdhogar.Value))

                Dim reader As IDataReader = cmd.ExecuteReader()

                

                reader.NextResult()


                 With Chart2

                    .DataSource = reader
                    .ImageType = DataVisualization.Charting.ChartImageType.Png


                    With Chart2.Series("Default")
                        .XValueMember = "Identificador"
                        .YValueMembers = "li"

                        .ChartType = SeriesChartType.Column
                        .LabelFormat = "P2"
                        .Item("RadarDrawingStyle") = "Area"
                        .Item("AreaDrawingStyle") = "Polygon"
                        .Item("CircularLabelsStyle") = "Horizontal"
                    End With


                    With Chart2.Series("v1")
                        .XValueMember = "Identificador"
                        .YValueMembers = "v1"

                        .ChartType = SeriesChartType.Column
                        .LabelFormat = "P2"
                        .Item("RadarDrawingStyle") = "Area"
                        .Item("AreaDrawingStyle") = "Polygon"
                        .Item("CircularLabelsStyle") = "Horizontal"

                    End With

                    With Chart2.Series("v2")
                        .XValueMember = "Identificador"
                        .YValueMembers = "v2"

                        .ChartType = SeriesChartType.Column
                        .LabelFormat = "P2"
                        .Item("RadarDrawingStyle") = "Area"
                        .Item("AreaDrawingStyle") = "Polygon"
                        .Item("CircularLabelsStyle") = "Horizontal"
                    End With

                    With Chart2.Series("v3")
                        .XValueMember = "Identificador"
                        .YValueMembers = "v3"

                        .ChartType = SeriesChartType.Column
                        .LabelFormat = "P2"
                        .Item("RadarDrawingStyle") = "Area"
                        .Item("AreaDrawingStyle") = "Polygon"
                        .Item("CircularLabelsStyle") = "Horizontal"
                    End With
                    'Chart1.Series("Default").XValueMember = "Identificador"
                    'Chart1.Series("Default").YValueMembers = "Porcentaje"
                    Chart2.DataBind()
                    'Chart1.Series("Default").ChartType = SeriesChartType.Radar
                    ''Chart1.Series("Default").ChartType = SeriesChartType.Line
                    'Chart1.Series("Default").LabelFormat = "P2"
                    'Chart1.Series("Default")("RadarDrawingStyle") = "Area"
                    'Chart1.Series("Default")("AreaDrawingStyle") = "Polygon"
                    'Chart1.Series("Default")("CircularLabelsStyle") = "Horizontal"



                    '.Series("Default").XValueMember = "Identificador"
                    '.Series("Default").YValueMembers = "Porcentaje"
                    '.DataBind()
                    '.Series("Default").ChartType = SeriesChartType.Column

                    '.Series("Default").LabelFormat = "P2"
                    .ChartAreas("ChartArea1").AxisY.LabelStyle.Format = "P2"
                    .ChartAreas("ChartArea1").AxisY.Maximum = 1

                    '' Set series point width
                    '.Series("Default")("PointWidth") = "0.6"

                    '' Show data points labels
                    '.Series("Default").IsValueShownAsLabel = False

                    '' Set data points label style
                    '.Series("Default")("BarLabelStyle") = "Center"

                    '' Show as 3D
                    '.ChartAreas("ChartArea1").Area3DStyle.Enable3D = True

                    '' Draw as 3D Cylinder
                    '.Series("Default")("DrawingStyle") = "Cylinder"



                    '.ChartAreas("ChartArea1").Area3DStyle.Enable3D = False

                End With

            End Using

            cnn.Close()

        End Using


        'Using bd As New bdReportesDataContext
        '    With Chart2

        '        .DataSource = bd.rptCategoriazacionXHogar(CInt(hdIdhogar.Value))
        '        .ImageType = DataVisualization.Charting.ChartImageType.Png
        '        .Series("Default").XValueMember = "Identificador"
        '        .Series("Default").YValueMembers = "Porcentaje"
        '        .DataBind()
        '        .Series("Default").ChartType = SeriesChartType.Column
        '        .Series("Default").LabelFormat = "P2"
        '        .ChartAreas("ChartArea1").AxisY.LabelStyle.Format = "P2"
        '        .ChartAreas("ChartArea1").AxisY.Maximum = 1

        '        ' Set series point width
        '        .Series("Default")("PointWidth") = "0.6"

        '        ' Show data points labels
        '        .Series("Default").IsValueShownAsLabel = False

        '        ' Set data points label style
        '        .Series("Default")("BarLabelStyle") = "Center"

        '        ' Show as 3D
        '        .ChartAreas("ChartArea1").Area3DStyle.Enable3D = True

        '        ' Draw as 3D Cylinder
        '        .Series("Default")("DrawingStyle") = "Cylinder"



        '        .ChartAreas("ChartArea1").Area3DStyle.Enable3D = False
        '    End With

        'End Using
    End Sub

    Sub DetalleIntegrantes()
        Using bd As New bdReportesDataContext
            grdDetalleintegrantes.DataSource = bd.rptCategoriazacionXHogarIntegrantesFamilia(CInt(hdIdhogar.Value))
            grdDetalleintegrantes.DataBind()
        End Using
    End Sub

    Sub PsicoSocialHogar()
        Using bd As New bdReportesDataContext
            GrdPsicoSocialHogar.DataSource = bd.rptCategoriazacionXHogarPsicosocialFamiliarComunitario(CInt(hdIdhogar.Value))
            GrdPsicoSocialHogar.DataBind()
        End Using
    End Sub
    Sub PsicoSocialIndividual()
        Using bd As New bdReportesDataContext
            grdPsicoSocialIndividual.DataSource = bd.rptCategoriazacionXHogarPsicosocialIndividual(CInt(hdIdhogar.Value))
            grdPsicoSocialIndividual.DataBind()
        End Using
    End Sub



    'Sub traerProyectos() ' carga el control ddlNombreProyecto con los proyectos que han ingresado

    '    Dim u As New Web_Usuario
    '    Using h As New Helper
    '        u = h.GetUsuario(Context.User.Identity.Name)
    '    End Using
    '    Using bd As New BdHogaresDataContext
    '        Dim a = From i In bd.Web_Proyectos
    '                Where (i.web_Operadore.idOperador = u.IdOperador) Or IsNothing(u.IdOperador)
    '                Select i.idProyecto, i.CodigoProyecto
    '        ddlProyectos.DataSource = a
    '        ddlProyectos.DataValueField = "idProyecto"
    '        ddlProyectos.DataTextField = "CodigoProyecto"
    '        ddlProyectos.DataBind()


    '        Dim b = From i In bd.Web_Parametros
    '                Where (i.Categoria = "visitas")
    '                Select i.NombreOpcion, i.ValorOpcion

    '        ddvisita.DataSource = b
    '        ddvisita.DataValueField = "NombreOpcion"
    '        ddvisita.DataTextField = "NombreOpcion"
    '        ddvisita.DataBind()
    '    End Using
    'End Sub

    'Protected Sub btnConsultar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConsultar.Click
    '    cargardatos()
    'End Sub
    'Sub cargardatos()
    '    Using bd As New bdReportesDataContext
    '        grdResultado.DataSource = bd.rptCategoriazacion(Context.User.Identity.Name, ddvisita.SelectedValue, ddlProyectos.SelectedValue)
    '        grdResultado.DataBind()
    '    End Using
    'End Sub
    Protected Sub btnExpt_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Handles btnExpt.Click

    End Sub

    Protected Sub exportarAExcel()


        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As StringWriter = New StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        Dim pagina As Page = New Page

        Dim form = New HtmlForm
        pagina.EnableEventValidation = False
        pagina.DesignerInitialize()
        pagina.Controls.Add(form)

        'form.Controls.Add(tabla)
        'form.Controls.Add(Me.grdResultado)
        pagina.RenderControl(htw)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Disposition", "attachment;filename=Reporte.xls")
        Response.Charset = "UTF-8"
        Response.ContentEncoding = Encoding.Default
        Response.Write(sb.ToString())
        Response.End()

    End Sub

    'Protected Sub btnExp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExp.Click
    '    'cargardatos()
    '    exportarAExcel()
    'End Sub
End Class
